<template>
  <div class="table">
    <div class="crumbs">
      <i class="el-icon-tickets"></i>收藏信息
    </div>
    <div class="container">
      <div class="handle-box">
        <el-input v-model="select_word" placeholder="筛选关键词" class="handle-input"></el-input>
        <el-button type="danger"  @click="deleteCollectAll">批量删除</el-button>
      </div>
    </div>
    <el-table size="mini" ref="multipleTable" border style="width:100%" height="680px" :data="tableData" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="40"></el-table-column>
      <el-table-column prop="name" label="歌手-歌名" align="center"></el-table-column>
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button  type="danger" size="mini" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog title="删除歌曲" :visible.sync="DeleteVisible" width="450px" center>
      <div align="center">删除不可恢复，是否确定删除？</div>
      <span slot="footer">
                <el-button @click="DeleteVisible = false">取消</el-button>
                <el-button  type="danger" @click="DelteCollectRow">确定</el-button>
            </span>
    </el-dialog>
  </div>
</template>

<script>
import {mixin} from "../mixinx/index";
import {
  delteCollect,
  delteListSong,
  getCollectOfUserId,
  getListSongDetail,
  setListSongAdd,
  SongofSongId,
  SongofSongName
} from '../api/index';

export default {
  mixins: [mixin],
  name:'Collect',
  props:['id'],
  data(){
    return{
      centerDialogVisible: false, //添加弹窗是否显示
      DeleteVisible: false,          //删除弹窗是否显示
      registerForm:{      //添加框
        singerName: '',     //歌手名字
        songName: ''        //歌曲名字
      },
      tableData: [],
      tempData: [],
      select_word: '',
      idx: -1,          //当前选择项
      multipleSelection: [],   //哪些项已经打勾

    }
  },
  watch:{
    //搜索框里面的内容发生变化的时候，搜索结果table列表的内容跟着它的内容发生变化
    select_word: function(){
      if(this.select_word == ''){
        this.tableData = this.tempData;
      }else{
        this.tableData = [];
        for(let item of this.tempData){
          if(item.name.includes(this.select_word)){
            this.tableData.push(item);
          }
        }
      }
    }
  },
  created(){
    this.getData();
  },
  methods:{
    //查询用户的所有收藏
    getData(){
      this.tempData = [];
      this.tableData = [];
      getCollectOfUserId(this.$route.query.id).then(res => {
        for(let item of res){
          this.getSong(item.song_id);
        }
      })
    },
    //根据歌曲id查询歌曲对象，放到tempData和tableData里面
    getSong(id){
      SongofSongId(id)
        .then(res => {
          this.tempData.push(res);
          this.tableData.push(res);
        })
        .catch(err => {
          console.log(err);
        });
    },
    //删除一条收藏
    DelteCollectRow(){
      delteCollect(this.$route.query.id,this.idx.id)
        .then(res => {
          if(res){
            this.getData();
            this.notify("删除成功","success");
          }else{
            this.notify("删除失败","error");
          }
        })
        .catch(err => {
          console.log(err);
        });
      this.DeleteVisible = false;
    },
    // 批量歌单歌曲删除，删除已选择的项
    deleteCollectAll() {
      for (let item of this.multipleSelection) {
        this.handleDelete(item)
        this.DelteCollectRow()
      }
      this.multipleSelection = []
    },
  }
}
</script>

<style scoped>
.handle-box{
  margin-bottom: 20px;
}

.handle-input{
  width: 300px;
  display: inline-block;
}

</style>
